package com.bjjnts;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.bjjnts.config.User;
import com.bjjnts.config.UserConfig;
import com.bjjnts.service.WatchService;
import com.bjjnts.tread.RunClientThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author junk_dog
 */
@Slf4j
@EnableScheduling
@SpringBootApplication
public class BjjntsApplication {

	public static void main(String[] args) {
		SpringApplication.run(BjjntsApplication.class, args);
	}


	@Resource
	private UserConfig userConfig;

	@Value("${face.path}")
	private String facePath;

	boolean leanFalse = false;

	private static final int TIME = 86400000;

	/**
	 * 每24小时执行
	 */
	@Scheduled(fixedDelay = TIME)
	public void run() {
		List<User> list = userConfig.getList();
		if (CollUtil.isEmpty(list)) {
			log.info("用户为空");
		}
		for (User user : list) {
			WatchService watchService = new WatchService(user.getUsername(), user.getPassword());
			watchService.setFacePath(facePath);
			List<JSONObject> taskList = watchService.getTaskList();
			RunClientThread runClientThread = new RunClientThread(taskList, watchService);
			runClientThread.setName("Tread-user-" + StrUtil.subWithLength(user.getUsername(), user.getUsername().length()-4, user.getUsername().length()));
			runClientThread.start();
		}
	}


}
